package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.bac.BacAstTestMthDict;
import com.ysd.lis.entity.bac.BacInterpretationCodeDict;
import com.ysd.lis.entity.sta.StaSensitiveDict;
import com.ysd.lis.mapper.bac.BacInterpretationCodeDictMapper;
import com.ysd.lis.request.bac.BacGermBaseDictRequest;
import com.ysd.lis.service.bac.BacInterpretationCodeDictService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author LuoXS
 * @date 2024/3/28
 * version 1.0
 */
@Service
public class BacInterpretationCodeDictServiceImpl extends ServiceImpl<BacInterpretationCodeDictMapper, BacInterpretationCodeDict> implements BacInterpretationCodeDictService {

    @Autowired
    BacInterpretationCodeDictMapper mapper;


    @Override
    public Result add(BacInterpretationCodeDict dict) {
        Result result = Result.succ(1, "添加成功！", null);
        try {
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                dict.setCreator(sysUser.getCreator());
                dict.setOrgId(sysUser.getOrgId());
            }
            boolean save = this.save(dict);
            if (!save) {
                result = Result.fail("添加失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail("添加失败！");
        }
        return result;
    }

    @Override
    public Result updateDict(BacInterpretationCodeDict dict) {
        boolean b = this.updateById(dict);
        if (b) {
            return Result.succ(1, "编辑成功！", null);
        } else {
            return Result.fail("编辑失败！");
        }
    }

    @Override
    public Result pageList(BacGermBaseDictRequest.BacInterpretationCodeDictPageParam pageParam) {
        Result result;
        try {
            LambdaQueryWrapper<BacInterpretationCodeDict> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            Page<BacInterpretationCodeDict> page = new Page<>();
            page.setCurrent(pageParam.getPageIndex());
            page.setSize(pageParam.getPageSize());
            //参数
            if (!StringUtils.isEmpty(pageParam.getSearchValue())) {
                lambdaQueryWrapper.and(wrapper -> wrapper.like(BacInterpretationCodeDict::getName, pageParam.getSearchValue()).or().like(BacInterpretationCodeDict::getStandardName, pageParam.getSearchValue()));

            }
            lambdaQueryWrapper.eq(BacInterpretationCodeDict::getDelFlag, 0);
            lambdaQueryWrapper.orderByAsc(BacInterpretationCodeDict::getDisplayOrder);
            Page<BacInterpretationCodeDict> selectPage = mapper.selectPage(page, lambdaQueryWrapper);
            result = Result.succ(selectPage);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail("查询失败！");
        }
        return result;
    }

    @Override
    public Result findBacInterpretationCodeDictList(BacInterpretationCodeDict bacInterpretationCodeDict) {
        List<BacInterpretationCodeDict> list = findBacInterpretationCodeDictArray(bacInterpretationCodeDict);
        return Result.succ(1, "请求成功", list);
    }

    @Override
    public List<BacInterpretationCodeDict> findBacInterpretationCodeDictArray(BacInterpretationCodeDict bacInterpretationCodeDict) {
        MPJLambdaWrapper<BacInterpretationCodeDict> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(BacAstTestMthDict::getDelFlag, 0);
        queryWrapper.eq(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(bacInterpretationCodeDict.getCode()), BacInterpretationCodeDict::getCode, bacInterpretationCodeDict.getCode());
        queryWrapper.eq(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(bacInterpretationCodeDict.getName()), BacInterpretationCodeDict::getName, bacInterpretationCodeDict.getName());
        queryWrapper.eq(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(bacInterpretationCodeDict.getInputCode()), BacInterpretationCodeDict::getInputCode, bacInterpretationCodeDict.getInputCode());
        return mapper.selectList(queryWrapper);
    }

    @Override
    public Result delete(BacGermBaseDictRequest.BacInterpretationCodeDictDeleteParam deleteParam) {
        Result result;
        try {
            if (ToolsUtils.isEmpty(deleteParam) || ToolsUtils.isEmpty(deleteParam.getIds())) {
                return Result.fail("入参不可为空！");
            }
            String[] array = deleteParam.getIds().split(",");
            boolean b = this.removeByIds(Arrays.asList(array));
            if (b) {
                result = Result.succ(1, "删除成功", null);
            } else {
                result = Result.fail(400, "删除失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", "");
        }
        return result;
    }

    @Override
    public Result staDataSyncBacInterCode(List<StaSensitiveDict> staSensitiveDicts) {
        for (StaSensitiveDict staSensitiveDict : staSensitiveDicts) {
            BacInterpretationCodeDict bacInterpretationCodeDict = new BacInterpretationCodeDict();
            BeanUtils.copyProperties(staSensitiveDict, bacInterpretationCodeDict,"id");
            bacInterpretationCodeDict.setCode(staSensitiveDict.getSensitiveNo());
            bacInterpretationCodeDict.setName(staSensitiveDict.getSensitiveNa());
            this.saveOrUpdate(bacInterpretationCodeDict,new LambdaQueryWrapper<BacInterpretationCodeDict>().eq(BacInterpretationCodeDict::getCode,bacInterpretationCodeDict.getCode()).eq(BacInterpretationCodeDict::getDelFlag,0));
        }
        return Result.succ(1,"同步成功！",null);
    }
}
